<?php

namespace upload;

use think\facade\Filesystem;
use think\File;


class WechatUpload
{
    /**
     * 文件上传(微信素材上传)
     * @param File $file
     * @param string $folder
     * @param int $new_width
     * @param int $new_height
     * @param $percent
     * @return array
     */
    public static function uploadFile(File $file, string $folder, int $new_width, int $new_height, $percent): array
    {
        //数据验证
        validate(['imgFile' => [
            'fileSize' => 16388608,
            'fileExt' => 'jpg,jpeg,png,mp4,mp3,wma,wav,amr',
            'fileMime' => 'image/jpeg,image/png,video/mp4,audio/mp3,audio/mpeg', //这个一定要加上
        ]])->check(['imgFile' => $file]);
        //保存文件
        $savename = Filesystem::putFile('', $file);
        //获取文件完整路径
        $savename = Filesystem::path($savename);
        //获取文件后缀名
        $suffix = pathinfo($savename)['extension'];
        $file_size = @filesize($savename) / 1024;
        //文件授权777
        chmod($savename, 0777);
        return [
            'file_size' => intval($file_size),
            'path' => $savename,
            'suffix' => $suffix,
        ];
    }
}